package com.ikangtai.shecare;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class OverviewActivity extends AppCompatActivity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.overview_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initView();
    }

    private final void initView()
    {
        webview = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webview.requestFocus();
        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());

        // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法
        webview.addJavascriptInterface(new CustomerData(), "CustomerData");
        webview.addJavascriptInterface(new BBTChartClick(this), "bbtChartClickListener");

        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false); //垂直不显示

        //找到Html文件，也可以用网络上的文件
        webSettings.setDomStorageEnabled(true);
        webview.loadUrl("file:///android_asset/health/overview.html");
    }

    private final class CustomerData{
        private String customerDataJSONStr;

        public CustomerData(){
            customerDataJSONStr = "["
                    + "{\"date\": \"2015-12-5\", "
                    + "\"period\": \"yjq\", "
                    + "\"temperature\": \"37.5\", "
                    + "\"heart\":\"yes\", "
                    + "\"moon\":\"no\",\"drink\":\"no\", "
                    + "\"medicine\":\"yes\", "
                    + "\"plane\":\"no\", "
                    + "\"mucus\":\"3\""
                    + "},"
                    + "{\"date\": \"2015-12-6\", "
                    + "\"period\": \"lpq\", "
                    + "\"temperature\": \"38.5\", "
                    + "\"heart\":\"no\", "
                    + "\"moon\":\"no\",\"drink\":\"yes\", "
                    + "\"medicine\":\"yes\", "
                    + "\"plane\":\"no\", "
                    + "\"mucus\":\"4\""
                    + "},"
                    + "{\"date\": \"2015-12-7\", "
                    + "\"period\": \"plq\", "
                    + "\"temperature\": \"38.5\", "
                    + "\"heart\":\"no\", "
                    + "\"moon\":\"no\",\"drink\":\"yes\", "
                    + "\"medicine\":\"yes\", "
                    + "\"plane\":\"no\", "
                    + "\"mucus\":\"4\""
                    + "},"
                    + "{\"date\": \"2015-12-8\", "
                    + "\"period\": \"htq\", "
                    + "\"temperature\": \"37.5\", "
                    + "\"heart\":\"yes\", "
                    + "\"moon\":\"no\",\"drink\":\"no\", "
                    + "\"medicine\":\"yes\", "
                    + "\"plane\":\"no\", "
                    + "\"mucus\":\"3\""
                    + "},"
                    + "{\"date\": \"2015-12-9\", "
                    + "\"period\": \"yjq\", "
                    + "\"temperature\": \"37.5\", "
                    + "\"heart\":\"yes\", "
                    + "\"moon\":\"no\",\"drink\":\"no\", "
                    + "\"medicine\":\"yes\", "
                    + "\"plane\":\"no\", "
                    + "\"mucus\":\"3\""
                    + "}"
                    + "]";
        }

        @JavascriptInterface
        public String getCustomerDataJSONStr(){
            return customerDataJSONStr;
        }
    }

    public class BBTChartClick{

        private Context context;

        public BBTChartClick(Context context){
            this.context = context;
        }

        @JavascriptInterface
        public void openNewActivity(){
            Intent intent = new Intent();
            intent.setClass(context, BBTChartActivity.class);
            context.startActivity(intent);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) { //监控/拦截/屏蔽返回键
            webview.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //必须捕捉，否则Activity onDestroy然后onCreate，js不能有效的监听到横竖屏的变化;
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // 当新设置中，屏幕布局模式为横排时
        if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            //TODO 横屏，调整webview为全屏
        }
        else{
            //TODO 竖屏，调整webview和下半部分的比例
        }
        super.onConfigurationChanged(newConfig);
    }

    //防止内存泄露
    @Override
    protected void onDestroy(){
        webview.destroy();
        webview.removeAllViews();
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_health, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
